package com.lijy.dom;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.Test;

public class DomTreeFrame extends JFrame {
    private static final int DEFAUTL_WIDTH = 400;
    private static final int DEFAUTL_HEIGHT = 400;

    private DocumentBuilder builder;

    public DomTreeFrame() {
        setSize(DEFAUTL_WIDTH, DEFAUTL_HEIGHT);

        JMenu fileMenu = new JMenu("File");
        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        //TODO
                    }
                }
        );
        fileMenu.add(openItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        System.exit(0);
                    }
                }
        );
        fileMenu.add(exitItem);

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
    }

    public void openFile() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("dom"));

        chooser.setFileFilter(
                new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
                    }

                    @Override
                    public String getDescription() {
                        return "XML files";
                    }
                }
        );
        int r = chooser.showOpenDialog(this);
        if (r != JFileChooser.APPROVE_OPTION) {
            return;
        }
        final File file = chooser.getSelectedFile();
//        new SwingWorker<Document, Void>() {
//            @Override
//            protected Document doInBackground() throws Exception {
//                if (builder == null) {
//                    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//                    builder = factory.newDocumentBuilder();
//                }
//                return (Document) builder.parse(file);
//            }
//
//            @Override
//            protected void done() {
//                try {
//                    Document doc = get();
//                    JTree tree = new JTree(new DomTreeModel(doc));
//                    tree.setCellRenderer(new DomTreeCellRenderer());
//
//                    setContentPane(new JScrollPane(tree));
//                    validate();
//                }
//                catch (Exception e) {
//                    JOptionPane.showMessageDialog(DomTreeFrame.this, e);
//                }
//                super.done();
//            }
//        }.execute();
    }



}
